﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ZLDC.Topo.BLL.Estate
{
    public class lp_Appointment
    {
        public static void GetList(int Pcount, Repeater RpList, string txtTime)
        {
            DataSet dt = ZLDC.Topo.BLL.Estate.lp_Basis.GetDept("");
            Pcount = dt.Tables[0].Rows.Count;
            RpList.DataSource = dt;
            RpList.DataBind();
            DataSet ds1 = ZLDC.Topo.BLL.Estate.lp_Basis.Getds("select * from lp_Configuration_main where deptID=" + _Utility.GetInfoBySession().m_sDeptID + " and CONVERT(varchar,workday,23) = '" + Convert.ToDateTime(txtTime).ToString("yyyy-MM-dd") + "'");

            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                foreach (RepeaterItem Item in RpList.Items)
                {
                    HiddenField hidid = (HiddenField)Item.FindControl("hidid");                    CheckBox chk = (CheckBox)Item.FindControl("CheckBox2");
                    CheckBox chk1 = (CheckBox)Item.FindControl("CheckBox1");
                    TextBox txtRemark = (TextBox)Item.FindControl("txtRemark");
                    HiddenField hidSoure = (HiddenField)Item.FindControl("hidSoure");
                    HiddenField hidtf = (HiddenField)Item.FindControl("hidtf");                    if (ds1.Tables[0].Rows[i]["userID"].ToString() == hidid.Value)
                    {
                        if (ds1.Tables[0].Rows[i]["choiceType"].ToString() == "1")
                        {
                            chk.Checked = true;
                        }
                        if (ds1.Tables[0].Rows[i]["leave"].ToString() == "1")
                        {
                            chk1.Checked = true;
                        }
                        chk.Enabled = false;
                        chk1.Enabled = false;
                        txtRemark.Enabled = false;
                        hidSoure.Value = "1";
                        string sql_reply = "select * from lp_main_Reply where Configuration_mainID=" + ds1.Tables[0].Rows[i]["id"].ToString() + " and UserObj=" + hidid.Value + " and CreateUserID=" + _Utility.GetInfoBySession().m_sUserID + " and txtTime='" + Convert.ToDateTime(txtTime).ToString("yyyy-MM-dd") + "' order by createtime desc";
                        DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql_reply);
                        if (ZLDC.Topo.BLL.Estate.lp_Basis.rtcount(ds))
                        {
                            txtRemark.Text = ds.Tables[0].Rows[0]["Reply"].ToString();
                            hidtf.Value = "1";
                        }
                        break;
                    }
                    
                   
                  
                }
            }
        }

        public static void BtnAdd(string txtTime, Repeater RpList, int Ppcount, int Pcount,Page p,string txtendtime)
        {
            
            DateTime today = Convert.ToDateTime(txtTime);
            DateTime startT = Convert.ToDateTime(txtTime);
            DateTime endT = Convert.ToDateTime(txtendtime);
            System.TimeSpan span = endT - startT;
            for (int i = 0; i <= int.Parse(span.Days.ToString()); i++)
            {
                BtnAdd_main(startT.AddDays(i).ToString("yyyy-MM-dd"), RpList, Ppcount, Pcount, p);
            }
          
        }

        public static void BtnAdd_main(string txtTime, Repeater RpList, int Ppcount, int Pcount, Page p)
        {
            string sql = " declare @mainid int;";
            string createUser = _Utility.GetInfoBySession().m_sUserID;            string[] deptlist = ZLDC.Topo.BLL.Estate.lp_Basis.GetDeptExprlist(_Utility.GetInfoBySession().m_sUserID);            string expr1 = deptlist[6];
            string expr2 = deptlist[7];
            string expr3 = deptlist[8];
            string expr4 = deptlist[9];
            string expr5 = deptlist[10];
            string expr6 = deptlist[11];
            foreach (RepeaterItem Item in RpList.Items)
            {
                HiddenField hidid = (HiddenField)Item.FindControl("hidid");                HiddenField hiddeptid = (HiddenField)Item.FindControl("hiddeptid");                CheckBox chk = (CheckBox)Item.FindControl("CheckBox2");
                CheckBox chk1 = (CheckBox)Item.FindControl("CheckBox1");
                TextBox txtRemark = (TextBox)Item.FindControl("txtRemark");
                HiddenField hidSoure = (HiddenField)Item.FindControl("hidSoure");
                HiddenField hidname = (HiddenField)Item.FindControl("hidname");                HiddenField hidtf = (HiddenField)Item.FindControl("hidtf");                if (hidSoure.Value != "1")
                {
                    if (chk.Checked || chk1.Checked)
                    {
                        
                        sql += " insert into lp_Configuration_main(";
                        sql += "workDay,userID,deptid,total,sureScore,color,isConfirm,isComplete,leave,choiceType,remark,createTime,createUser,Other1,Other2,Other10,Other4,Other5,Other6,Other7,Other8,Other9,Other3";
                        sql += ")";
                        sql += " values (";
                        sql += "'" + txtTime + "'," + hidid.Value + "," + hiddeptid.Value + ",0,0,1,0,0," + (chk1.Checked == true ? "1" : "0") + "," + (chk.Checked == true ? "1" : "0") + ",'" + txtRemark.Text + "',";
                        sql += "'" + txtTime + "'," + _Utility.GetInfoBySession().m_sUserID + ",'','" + hidname .Value+ "','','" +expr1 + "','" + expr2 + "','" + expr3 + "','" + expr4 + "','" + expr5 + "','" + expr6 + "','"+_Utility.GetInfoBySession().m_sDeptName+"'";

                        sql += ");select @mainid=@@IDENTITY; ";
                        sql += " insert into lp_Configuration (mainId,BasisName,BasisCode,Point,SystemSouce,Other1,Other3,Other2,userID,CreateTime ";

                        sql += " ,Other4,Other5,Other6,Other7,Other8,Other9,Other10,SingleNumber)";
                        sql += "select @mainid,Name,TypeCode,Point,Highest,'0',TypeName," + _Utility.GetInfoBySession().m_sDeptID + "," + hidid.Value + ",'" + txtTime + "'";
                        sql += ",'" + expr1 + "','" + expr2 + "','" + expr3 + "','" + expr4 + "','" + expr5 + "','" + expr6 + "','" + _Utility.GetInfoBySession().m_sDeptName + "',Quantity";
                        sql+=" from lp_Basis; ";
                        Ppcount++;

                        #region 添加或者修改 工作意见
                        if (txtRemark.Text != "")
                        {
                            if (hidtf.Value != "1")
                            {
                                sql += " insert into lp_main_Reply (Configuration_mainID,Reply,MainID,DeptID,DeptName,Station,CreateTime,CreateName,txtTime,CreateUserid,UserObj) "
                                       + " values ("
                                       + "  @mainid,'" + txtRemark.Text + "',0," + _Utility.GetInfoBySession().m_sDeptID + ",'" + _Utility.GetInfoBySession().m_sDeptName + "',"
                                       + " '" + _Utility.GetInfoBySession().m_sStation + "' , '" + DateTime.Now + "','" + _Utility.GetInfoBySession().m_sUserName + "','" + txtTime + "'," + _Utility.GetInfoBySession().m_sUserID + "," + hidid.Value + ""
                                       + ");";
                            }
                            else
                            {

                                sql += "update lp_main_Reply set Reply='" + txtRemark.Text + "',UpdateTime='" + DateTime.Now.ToString("yyyy-MM-dd") + "' where  Configuration_mainID=select @@IDENTITY and UserObj=" + hidid.Value + " and CreateUserID=" + _Utility.GetInfoBySession().m_sUserID + " and Convert(varchar,txtTime,23)='" + txtTime + "'; ";
                            }
                        }
                        sql += " select @mainid=0;";
                        #endregion
                    }

                }
                else
                {
                    Ppcount++;
                }

                Pcount++;
            }
            if (sql != "")
            {
                ZLDC.Topo.BLL.Estate.lp_Basis.Execution_sql(sql);
            }
            string alert = "";
            if (Ppcount != Pcount)
            {
                alert = "已预约安排：" + (Ppcount) + "人，未安排：" + (Pcount - Ppcount) + "人";
            }
            else
            {
                alert = "布置工作任务已全部完成！";
            }
            p.Response.Write("<script>alert('" + alert + "！');window.location.href=location.href;</script>");
        }


        public static int Exists(string starttime,string endtime,string deptid,Page p,System.Web.UI.HtmlControls.HtmlAnchor aexists,Repeater rp)
        {
            string sql = "";
            sql = " select workDay from lp_Configuration_main where deptid = " + deptid + " and (workday>='" + starttime + "' and workday<='" + endtime + "') group by workday";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                string txttime = "";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    txttime += " " + Convert.ToDateTime(ds.Tables[0].Rows[i]["workDay"]).ToString("yyyy-MM-dd");
                }
                rp.DataSource = null;
                rp.DataBind();
                aexists.InnerHtml = "已有" + txttime + "安排预约，请重新选择日期或选择该日期做分配修改！";
                return ds.Tables[0].Rows.Count;
            }
            else
            {
                aexists.InnerHtml = "";
                return 0;
            }
        }
    }
}
